﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MazeRaider
{
    class MazeMap
    {
        private int? minX;
        private int? maxX;
        private int? minY;
        private int? maxY;
        private char[,] map = new char[2000, 2000];

        public MazeMap()
        {
            for (int i = 0; i < 2000; i++)
            {
                for (int j = 0; j < 2000; j++)
                {
                    map[i, j] = ' ';
                }
            }
        }

        private int coordToIndex(double coord)
        {
            double coo = Math.Round(coord, 1);
            return ((int)(coo * 10.0)) + 1000;
        }

        private void AddPoint(double x, double y, char c)
        {
            Console.WriteLine("Adding point [" + x + ", " + y + ", " + c + "]");

            int cx = coordToIndex(x);
            int cy = coordToIndex(y);

            if (minX == null || cx < minX)
            {
                minX = cx;
            }
            if (maxX == null || cx > maxX)
            {
                maxX = cx;
            }
            if (minY == null || cy < minY)
            {
                minY = cy;
            }
            if (maxY == null || cy > maxY)
            {
                maxY = cy;
            }

            map[cx, cy] = c;
        }

        public void AddStartingPoint(double x, double y)
        {
            AddPoint(x, y, 'S');
        }

        public void AddPassage(double x, double y)
        {
            AddPoint(x, y, '.');
        }

        public void AddObstacle(double x, double y)
        {
            AddPoint(x, y, 'X');
        }

        public bool IsVisited(double x, double y)
        {
            int cx = coordToIndex(x);
            int cy = coordToIndex(y);
            return !' '.Equals(map[cx, cy]);
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Maze:\n");
            for (int i = (int)maxX; i >= minX; i--)
            {
                for (int j = (int)minY; j <= maxY; j++)
                {
                    sb.Append(map[i, j]);
                }
                sb.Append("\n");
            }
            return sb.ToString();
        }
    }
}
